var map = {};//设置json
$(function(){
    selectjson(0);
})

var selectjson = function(t){
    var id = getQueryString("classroomid");
    //var id = 2020101;
    $("#classroomid").val(id);
    $.post("selectCurriculumByWeek",{
        classroomid:id,
        week:t
    },function(o){
        o = JSON.parse(o);

        if((o.state == 0 && o.data != undefined)||(o.state == 100)){
            map={};
            $("#currientWeek").val(o.zc);
            $("#xnxqhh").val(o.xnxqh);
            var maxJc = o.jc;
            $("#rolltype option[value='"+o.zc+"']").attr("selected","selected");
            if(o.data!=undefined){
                for(i in o.data){
                    map[o.data[i].lessonjc +"s"+o.data[i].week]=o.data[i];//设置坐标的值，并把坐标作为JSON属性名称
                }
            }
            $("#curriculum").html("<tr><th></th><th>星期一</th><th>星期二</th><th>星期三</th><th>星期四</th><th>星期五</th><th>星期六</th><th>星期日</th></tr>");
            for( var j= 0;j < maxJc;j++){
                var a="无",b="无",c="无",d="无",e="无",f="无",g="无";
                var tt = j +1;
                var a1 = tt+"s1";
                var b1 = tt+"s2";
                var c1 = tt+"s3";
                var d1 = tt+"s4";
                var e1 = tt+"s5";
                var f1 = tt+"s6";
                var g1 = tt+"s7";

                if(map[tt+"s1"]!=undefined){
                    a = map[tt+"s1"].lessonname ;
                }
                if(map[tt+"s2"]!=undefined){
                    b = map[tt+"s2"].lessonname ;
                }
                if(map[tt+"s3"]!=undefined){
                    c = map[tt+"s3"].lessonname ;
                }
                if(map[tt+"s4"]!=undefined){
                    d = map[tt+"s4"].lessonname ;
                }
                if(map[tt+"s5"]!=undefined){
                    e = map[tt+"s5"].lessonname ;
                }
                if(map[tt+"s6"]!=undefined){
                    f = map[tt+"s6"].lessonname ;
                }
                if(map[tt+"s7"]!=undefined){
                    g = map[tt+"s7"].lessonname ;
                }

                $("#curriculum").append('<tr><td>第'+tt+'节</td><td><button class="bt" onclick="curriculumdisplay(\''+a1+'\')" id ="'+tt+'s1">'+a+'</button></td>'
                    +'<td><button class="bt"  onclick="curriculumdisplay(\''+b1+'\')" id ="'+tt+'s2">'+b+'</button></td>'
                    +'<td><button class="bt"  onclick="curriculumdisplay(\''+c1+'\')" id ="'+tt+'s3">'+c+'</button></td>'
                    +'<td><button class="bt"  onclick="curriculumdisplay(\''+d1+'\')" id ="'+tt+'s4">'+d+'</button></td>'
                    +'<td><button class="bt"  onclick="curriculumdisplay(\''+e1+'\')" id ="'+tt+'s5">'+e+'</button></td>'
                    +'<td><button class="bt"  onclick="curriculumdisplay(\''+f1+'\')" id ="'+tt+'s6">'+f+'</button></td>'
                    +'<td><button class="bt"  onclick="curriculumdisplay(\''+g1+'\')" id ="'+tt+'s7">'+g+'</button></td>'
                    +'</tr>');
            }

        }else{
            alert("当前教室在本学期没有排课");
            $("#curriculum").html("<tr><th></th><th>星期一</th><th>星期二</th><th>星期三</th><th>星期四</th><th>星期五</th><th>星期六</th><th>星期日</th></tr>");
            return null;
        }
    })
}


var curriculumdisplay = function(t){
    if(map[t]==undefined){
        $("#lesson").val("");
        $("#teacher").val("");
        $("#subclass").val("");
        $("#ws1").val("");
        $("#we1").val("");
        $("#ws2").val("");
        $("#we2").val("");
        $("#hid").val(t);
        alert("请添加相关的信息");
        $("#tijiao").css("display","block");
        $("#del").css("display","none");
        return null;
    }
    $("#hid").val(t);
    $("#lesson").val(map[t].lessonname);
    $("#teacher").val(map[t].username);
    $("#subclass").val(map[t].subclass);
    $("#ws1").val(map[t].weekstart);
    $("#we1").val(map[t].weekend);
    alert("已有的课程只能查看和删除，不能修改");
    $("#tijiao").css("display","none");
    $("#del").css("display","block");
}

var updateWeek = function(){
    var tung = $("#rolltype option:selected").val();
    //var id = getQueryString("classroomid");
    var id = 2020101;
    selectjson(tung);
}

//老师的点击事件
var dianji =function(){
    //添加参会人员---》显示
    $("#toSensor").modal({backdrop:"static"}).modal('show');
    var teacherid = $("#teacherid").val();
    var teachername = $("#teachername").val();
    display("","");


}

var display =function(teacherid,teachername){
    $.ajax({
        url:"selectUserRole",
        type:"post",
        dataType:"json",
        data:{
            "role":0,
            "userid":teacherid,
            "username":teachername,
            "pageIndex":0,
            "pageEnd":100
        },
        error: function(){
            console.log("查询出错");
        },
        success:function(data){
            $("#monitorList02").html("<tr><td width=8%>选择</td><td width=20%>用户名</td><td width=20%>名字</td></tr>");
            if(data.state==100){
                alert("没有查询到数据");
                return null;
            }
            var list=data.data;
            for(var i=0;i<list.length;i++){
                var id=list[i].userid ;
                var username = list[i].username;

                $("#monitorList02").append("<tr><td><input name=\"ckb\" type=\"checkbox\"   sensor-name="+username+" sensor-id="+id+" /></td><td>" +
                    id+"</td><td>"+username+"</td></tr>  ");
            }
        }
    })
}
var searchTeacher = function(){
    var teacherid = $("#teacherid").val();
    var teachername = $("#teachername").val();
    if(teacherid ==undefined&&teachername==undefined){
        alert("查询条件不能为空");
        return null;
    }
    display(teacherid,teachername);
}

var xml;
function addSensors(){
    $(xml).find("sensor").remove();
    temp = "";
    tempname = "";
    $("input[name='ckb']").each(function(){

        if($(this).is(":checked")){
            var id=$(this).attr("sensor-id");
            var name=$(this).attr("sensor-name");
            if(temp == ""){
                temp = id;
                tempname = name;
            }else{
                //情况一，如果只是需要选择一个复选框，执行如下
                alert("只能选择一个老师");
                return null;
                //情况二，如果需要同时选中多个复选框，执行下面两行代码
//	    		temp = temp + "|"+id;
//	    		tempname = tempname +"#"+name;
            }
        }
    })
    if(temp!=""){
        $("#teaid").val(temp);
        $("#teacher").val(tempname);
    }

}

var tijiao = function(){
    if($("#hid").val()==undefined||$("#hid").val()==""){
        alert("请先选择一节课");
        return null;
    }
    var lessonname = $("#lesson").val();
    if(lessonname==undefined||lessonname==""){
        alert("课程名称不能为空");
        return null;
    }
    var username = $("#teacher").val();
    if(username==undefined||username==""){
        alert("老师名称不能为空");
        return null;
    }
    var userid = $("#teaid").val();
    var zc = $("#currientWeek").val();
    var xnxqh = $("#xnxqhh").val();
    var classroomid = $("#classroomid").val();
    var lessonjc = $("#hid").val().split("s")[0];
    var xq = $("#hid").val().split("s")[1];
    var ws1 = $("#ws1").val();
    var we1 = $("#we1").val();
    if(ws1==undefined||ws1==""||we1==undefined||we1==""){
        alert("起始周和结束周任意一个都不能为空");
        return null;
    }
    if(parseInt(ws1) >=parseInt(we1)){
        alert("起始周不能大于结束周");
        return null;
    }
    var subclass = $("#subclass").val();
    if(subclass==undefined||subclass==""){
        alert("班级不能为空");
        return null;
    }
    var ws2 = $("#ws2").val();
    var we2 = $("#we2").val();
    var wss2,wee2;
    if(ws2==undefined||ws2==""){
        if(!(we2==undefined||we2=="")){
            alert("第二种情况下的起始周和结束周必须同时存在或不存在");
            return null;
        }
    }else{
        if(!(we2==undefined||we2=="")){
            wss2 = ws2;
            wee2 = we2;

            if(parseInt(ws2) >=parseInt(we2)){
                alert("起始周不能大于结束周");
                return null;
            }

            if((parseInt(ws1)<=parseInt(ws2))&&(parseInt(ws2)<=parseInt(we1))){
                alert("两次的起始周与结束周存在交叉");
                return null;
            }
            if((parseInt(ws1)<=parseInt(we2))&&(parseInt(we2)<=parseInt(we1))){
                alert("两次的起始周与结束周存在交叉");
                return null;
            }

        }else{
            alert("第二种情况下的起始周和结束周必须同时存在或不存在");
            return null;
        }
    }

    $.ajax({
        url:"addCurriculum",
        type:"post",
        dataType:"json",
        data:{
            "lessonname":lessonname,
            "username":username,
            "userid":userid,
            "zc":zc,
            "xnxqh":xnxqh,
            "lessonjc":lessonjc,
            "xq":xq,
            "ws1":ws1,
            "we1":we1,
            "subclass":subclass,
            "classroomid":classroomid,
            "wss2":wss2,
            "wee2":wee2
        },
        error: function(){
            console.log("查询出错");
        },
        success:function(data){
            if(data.state !=0){
                alert(data.msg);
                return null;
            }else{
                alert(data.msg);
                $("#lesson").val("");
                $("#teacher").val("");
                $("#subclass").val("");
                $("#ws1").val("");
                $("#we1").val("");
                $("#ws2").val("");
                $("#we2").val("");
                $("#hid").val("");
                selectjson($("#currientWeek").val());
            }

        }
    })
}

var del = function(){
    if($("#hid").val() == undefined){
        alert("请先选择一节课");
        return null;
    }
    if(map[$("#hid").val()]==undefined){
        alert("只有已有的课程才可以删除");
        return null;
    }
    var curriculumid = map[$("#hid").val()].curriculumid;
    $.ajax({
        url:"delCurruculum",
        type:"post",
        dataType:"json",
        data:{
            "curriculumid":curriculumid
        },
        error: function(){
            console.log("查询出错");
        },
        success:function(data){
            if(data.state !=0){
                alert(data.msg);
                return null;
            }else{
                alert(data.msg);
                $("#lesson").val("");
                $("#teacher").val("");
                $("#subclass").val("");
                $("#ws1").val("");
                $("#we1").val("");
                $("#ws2").val("");
                $("#we2").val("");
                $("#hid").val("");
                selectjson($("#currientWeek").val());
            }

        }
    })
}

function getQueryString(name) {
    var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)', 'i');
    var r = window.location.search.substr(1).match(reg);
    if (r != null) {
        return unescape(r[2]);
    }
    return null;
}
